package lab2;

import static org.junit.Assert.*;

import java.util.Iterator;
import java.util.NoSuchElementException;

import org.junit.Test;

public class MyTreeSetTest {

	@Test
	public void test_iteratorHasNext() {
		MyTreeSet myTreeSet1=new MyTreeSet();
		myTreeSet1.add(1);
		Iterator<Integer> iter=myTreeSet1.iterator();
		assertTrue(iter.hasNext());
		iter.next();
		assertFalse(iter.hasNext());
	}
	
	@Test
	public void test_iteratorNextException() {
		MyTreeSet myTreeSet1=new MyTreeSet();
		myTreeSet1.add(1);
		Iterator<Integer> iter=myTreeSet1.iterator();
		iter.next();
		try {
			iter.next();
			fail("Ожидается NoSuchElementException");
		} catch(NoSuchElementException e) {
			assertNull(e.getMessage());
		}
	}
	
	@Test
	public void test_iteratorNext() {
		MyTreeSet myTreeSet1=new MyTreeSet();
		myTreeSet1.add(1);
		myTreeSet1.add(2);
		Iterator<Integer> iter=myTreeSet1.iterator();
		assertTrue(iter.next()==1);
		assertTrue(iter.next()==2);			
	}
	
	@Test
	public void test_iteratorNullTree() {
		MyTreeSet myTreeSet1=new MyTreeSet();
		 Iterator<Integer> iter=myTreeSet1.iterator();
		assertTrue(iter==null);
	}
	
	@Test
	public void test_size() {
		MyTreeSet myTreeSet1=new MyTreeSet();
		assertTrue(myTreeSet1.size()==0);	
		myTreeSet1.add(1);
		assertTrue(myTreeSet1.size()==1);	
		myTreeSet1.add(2);
		assertTrue(myTreeSet1.size()==2);		
	}
	
	@Test
	public void test_add() {
		MyTreeSet myTreeSet1=new MyTreeSet();
		assertFalse(myTreeSet1.contains(1));
		assertTrue(myTreeSet1.add(1));
		assertTrue(myTreeSet1.contains(1));
		assertTrue(myTreeSet1.add(2));
		assertTrue(myTreeSet1.contains(2));			
		assertFalse(myTreeSet1.add(2));	
	}
	
	@Test
	public void test_addException() {
		MyTreeSet myTreeSet1=new MyTreeSet();
		myTreeSet1.add(1);
		try {
			myTreeSet1.add(null);
			fail("Ожидается NullPointerException");
		} catch(NullPointerException e) {
			assertNull(e.getMessage());
		}
	}
	
	@Test
	public void test_contains() {
		MyTreeSet myTreeSet1=new MyTreeSet();
		assertFalse(myTreeSet1.contains(1));
		myTreeSet1.add(1);
		assertTrue(myTreeSet1.contains(1));
		myTreeSet1.add(2);
		assertTrue(myTreeSet1.contains(2));	
		myTreeSet1.remove(1);
		assertFalse(myTreeSet1.contains(1));
		assertTrue(myTreeSet1.contains(2));			
	}
	
	@Test
	public void test_containsException() {
		MyTreeSet myTreeSet1=new MyTreeSet();
		try {
			myTreeSet1.contains(null);
			fail("Ожидается NullPointerException");
		} catch(NullPointerException e) {
			assertNull(e.getMessage());
		}
	}
	
	@Test
	public void test_remove() {
		MyTreeSet myTreeSet1=new MyTreeSet();
		myTreeSet1.add(1);
		myTreeSet1.remove(1);
		assertFalse(myTreeSet1.contains(1));	

		myTreeSet1.add(1);
		myTreeSet1.add(-2);
		myTreeSet1.remove(1);
		assertFalse(myTreeSet1.contains(1));
		assertTrue(myTreeSet1.contains(-2));		

		myTreeSet1.add(1);
		myTreeSet1.add(-2);
		myTreeSet1.add(2);
		myTreeSet1.remove(1);
		assertFalse(myTreeSet1.contains(1));
		assertTrue(myTreeSet1.contains(-2));		
		assertTrue(myTreeSet1.contains(2));		
		
		myTreeSet1.add(1);
		myTreeSet1.add(-2);
		myTreeSet1.add(2);
		myTreeSet1.add(5);
		myTreeSet1.remove(1);
		assertFalse(myTreeSet1.contains(1));
		assertTrue(myTreeSet1.contains(-2));		
		assertTrue(myTreeSet1.contains(2));		
		assertTrue(myTreeSet1.contains(5));			
	}
	
	@Test
	public void test_isEmpty() {
		MyTreeSet myTreeSet1=new MyTreeSet();
		assertTrue(myTreeSet1.isEmpty());
		myTreeSet1.add(1);
		assertFalse(myTreeSet1.isEmpty());
		myTreeSet1.add(2);
		assertFalse(myTreeSet1.isEmpty());		
	}

	@Test
	public void test_clear() {
		MyTreeSet myTreeSet1=new MyTreeSet();
		myTreeSet1.add(1);
		myTreeSet1.add(2);
		assertFalse(myTreeSet1.isEmpty());
		myTreeSet1.clear();
		assertTrue(myTreeSet1.isEmpty());
	}
	
	@Test
	public void test_toString() {
		MyTreeSet myTreeSet1=new MyTreeSet();
		assertEquals(myTreeSet1.toString(),"");
		myTreeSet1.add(2);
		assertEquals(myTreeSet1.toString(),"2 ");
		myTreeSet1.add(1);
		assertEquals(myTreeSet1.toString(),"1 2 ");
		myTreeSet1.add(3);
		assertEquals(myTreeSet1.toString(),"1 2 3 ");
	}
}
